package com.auxgroup.smarthome.event;

import com.alibaba.fastjson.JSONObject;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.dto.DeviceFaultDto;
import com.auxgroup.smarthome.service.DeviceFaultService;
import com.auxgroup.smarthome.service.DeviceInfoDBService;
import com.auxgroup.smarthome.snoti.events.IPushEvent;
import com.auxgroup.smarthome.utils.NewDateUtils;
import com.auxgroup.smarthome.utils.common.LOG;
import com.auxgroup.smarthome.vo.DeviceFaultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *故障事件snoti处理类
 * Created by lixiaoxiao on 17-9-5.
 **/
@Component
public class DeviceFaultEvent implements IPushEvent {

    @Autowired
    private DeviceFaultService deviceFaultService;

    @Autowired
    private DeviceInfoDBService deviceInfoDBService;

    @Override
    public void callback(String message) {
        JSONObject msg = JSONObject.parseObject(message);
        //String s = "{\"event_type\":\"attr_fault\",\"event_id\":\"5992ad9bde17b600011120e8\",\"product_key\":\"60c8cbbef8814de2951383f7040aef26\",\"data\":{\"attr_name\":\"dc_fan_current_detection_ad_protection\",\"value\":0,\"attr_displayname\":\"直流风机电流检测AD异常保护\"},\"group_id\":null,\"delivery_id\":3,\"created_at\":1.502865363557E9,\"cmd\":\"event_push\",\"did\":\"tjQ7NRS9KTCybXvtSn2RuT\",\"mac\":\"34ea34ced358\"}";
        LOG.info(this, "snoti故障推送消息："+msg.toString());
        JSONObject jsonObject = (JSONObject) msg.get("data");
        String faultName = jsonObject.getString("attr_name");
        String faultReason = jsonObject.getString("attr_displayname");
        int status = jsonObject.getIntValue("value");
        String mac = msg.getString("mac").toUpperCase();
        //根据mac查找绑定的用户名和手机号
        String username = "";
        String phone = "";
        String uid = "";
        DeviceFaultVo deviceFaultVo = deviceInfoDBService.findByMac(mac);
        if(deviceFaultVo != null){
            username = deviceFaultVo.getUsername();
            phone = deviceFaultVo.getPhone();
            uid = deviceFaultVo.getUid();
        }
        Double createdAt = msg.getDouble("created_at");
        String occurrenceTime = NewDateUtils.formatDoubleToDateTime(createdAt, "YYYY-MM-dd HH:mm:ss");
        String resolveTime = "";
        if(status == BusinessConstant.FAULT_RESOLVED.getValue()){
            resolveTime = occurrenceTime;
        }
        /**
         * 业务逻辑：
         * 根据mac查找未解决故障,如果不存在,则新增一条未解决故障
         */
        DeviceFaultDto deviceFaultDto = deviceFaultService.findDeviceFault(mac);
        if(deviceFaultDto == null){
            deviceFaultDto = new DeviceFaultDto(faultName, mac, faultReason,
                    occurrenceTime, resolveTime, username, phone, status,uid);
            deviceFaultService.saveDeviceFault(deviceFaultDto);
        }else{
            if(faultName.equals(deviceFaultDto.getFaultName())){
                deviceFaultDto.setStatus(status);
                deviceFaultDto.setPhone(phone);
                deviceFaultDto.setUsername(username);
                deviceFaultDto.setUid(uid);
                deviceFaultDto.setResolveTime(resolveTime);
                deviceFaultService.saveDeviceFault(deviceFaultDto);
            }else{
                //把未解决的置为已解决，并新增一条数据
                deviceFaultDto.setStatus(BusinessConstant.FAULT_RESOLVED.getValue());
                deviceFaultDto.setResolveTime(occurrenceTime);
                deviceFaultService.saveDeviceFault(deviceFaultDto);
                deviceFaultDto = new DeviceFaultDto(faultName, mac, faultReason,
                        occurrenceTime,resolveTime, username, phone, status,uid);
                deviceFaultService.saveDeviceFault(deviceFaultDto);
            }
        }
    }
}
